import { createRouter, createWebHistory } from 'vue-router'
import store from '@/store'

// 页面组件
import Home from '@/views/Home.vue'
import Login from '@/views/auth/Login.vue'
import Register from '@/views/auth/Register.vue'
import PetsList from '@/views/pets/PetsList.vue'
import PetDetails from '@/views/pets/PetDetails.vue'
import AdoptionForm from '@/views/adoptions/AdoptionForm.vue'
import MyApplications from '@/views/user/MyApplications.vue'
import MyPets from '@/views/user/MyPets.vue'
import UserProfile from '@/views/user/Profile.vue'

// 管理员页面
import AdminDashboard from '@/views/admin/Dashboard.vue'
import AdminPets from '@/views/admin/Pets.vue'
import AdminAddPet from '@/views/admin/AddPet.vue'
import AdminEditPet from '@/views/admin/EditPet.vue'
import AdminApplications from '@/views/admin/Applications.vue'
import AdminUsers from '@/views/admin/Users.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { guestOnly: true }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: { guestOnly: true }
  },
  {
    path: '/pets',
    name: 'PetsList',
    component: PetsList
  },
  {
    path: '/pets/:id',
    name: 'PetDetails',
    component: PetDetails,
    props: true
  },
  {
    path: '/pets/:id/adopt',
    name: 'AdoptionForm',
    component: AdoptionForm,
    meta: { requiresAuth: true },
    props: true
  },
  {
    path: '/my-applications',
    name: 'MyApplications',
    component: MyApplications,
    meta: { requiresAuth: true }
  },
  {
    path: '/my-pets',
    name: 'MyPets',
    component: MyPets,
    meta: { requiresAuth: true }
  },
  {
    path: '/profile',
    name: 'UserProfile',
    component: UserProfile,
    meta: { requiresAuth: true }
  },
  // 管理员路由
  {
    path: '/admin',
    name: 'AdminDashboard',
    component: AdminDashboard,
    meta: { requiresAuth: true, requiresAdmin: true }
  },
  {
    path: '/admin/pets',
    name: 'AdminPets',
    component: AdminPets,
    meta: { requiresAuth: true, requiresAdmin: true }
  },
  {
    path: '/admin/pets/add',
    name: 'AdminAddPet',
    component: AdminAddPet,
    meta: { requiresAuth: true, requiresAdmin: true }
  },
  {
    path: '/admin/pets/:id/edit',
    name: 'AdminEditPet',
    component: AdminEditPet,
    meta: { requiresAuth: true, requiresAdmin: true },
    props: true
  },
  {
    path: '/admin/applications',
    name: 'AdminApplications',
    component: AdminApplications,
    meta: { requiresAuth: true, requiresAdmin: true }
  },
  {
    path: '/admin/users',
    name: 'AdminUsers',
    component: AdminUsers,
    meta: { requiresAuth: true, requiresAdmin: true }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  const isLoggedIn = store.getters['auth/isAuthenticated']
  const isAdmin = store.getters['auth/isAdmin']
  
  // 检查是否需要身份验证
  if (to.matched.some(record => record.meta.requiresAuth) && !isLoggedIn) {
    next({ name: 'Login', query: { redirect: to.fullPath } })
  } 
  // 检查是否需要管理员权限
  else if (to.matched.some(record => record.meta.requiresAdmin) && !isAdmin) {
    next({ name: 'Home' })
  }
  // 检查是否为仅限游客的页面（登录、注册）
  else if (to.matched.some(record => record.meta.guestOnly) && isLoggedIn) {
    next({ name: 'Home' })
  } 
  else {
    next()
  }
})

export default router 